unit uColorDecoder;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uBaseCalcForm, ImgList, StdCtrls, ExtCtrls, ComCtrls;

type
  TfmColorDecoder = class(TfmBaseCalcForm)
    ImageList1: TImageList;
    rgStripCount: TRadioGroup;
    cbLine0: TComboBoxEx;
    cbLine1: TComboBoxEx;
    cbLine2: TComboBoxEx;
    cbLineMult: TComboBoxEx;
    cbLine6: TComboBoxEx;
    cbLineAcc: TComboBoxEx;
    Panel1: TPanel;
    Image1: TImage;
    Image2: TImage;
    imgLine0: TImage;
    imgLine1: TImage;
    imgLine2: TImage;
    imgLine3: TImage;
    imgLineAcc: TImage;
    imgLine6: TImage;
    laValue: TLabel;
    Shape1: TShape;
    procedure rgStripCountClick(Sender: TObject);
    procedure DoLineChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    class function CalcFriendlyName: string; override;
    class function RunKey: string; override;
    class procedure InvokeCalc; override;
    procedure AfterConstruction; override;
  end;

var
  fmColorDecoder: TfmColorDecoder;

implementation

{$R *.dfm}

uses uFormReg, uLocalization;

const
  cfMult: array[0..8] of single = (1,10,100,1e3,1e4,1e5,1e6,0.1,0.01);
  sfAcc: array[0..3] of single = (1,2,5,10);
  stResMult: array[0..8] of string = ('C0','C1','C2','C3','C4','C5','C6','CG','CS');
  stResAcc: array[0..3] of string = ('C1','C2','CG','CS');
  stResPPM: array[0..8] of string = ('C0','C1','C2','C4','C5','C3','C6','C7','C8');
  sfPPM: array[0..8] of single = (250,100,50,25,20,15,10,5,1);
  
var
  sfUnits: array[0..2] of string;

{ TfmColorDecoder }

procedure TfmColorDecoder.AfterConstruction;

    procedure UpdateColorText(aobj: TComboBoxEx);
    var i: integer;
    begin
    for i := 0 to aobj.ItemsEx.Count-1 do
      aobj.ItemsEx[i].Caption := GetLocaleString(aobj.ItemsEx[i].Caption);
    end;

begin
  inherited;
  sfUnits[0] := getString('#global\ohm');
  sfUnits[1] := getString('#global\kohm');
  sfUnits[2] := getString('#global\mohm');
  
  rgStripCount.Caption := GetLocaleString('countcaption');
  UpdateColorText(cbLine0);
  UpdateColorText(cbLine1);
  UpdateColorText(cbLine2);
  UpdateColorText(cbLineAcc);
  UpdateColorText(cbLineMult);
  UpdateColorText(cbLine6);

  cbLine0.ItemIndex := 0;
  cbLine1.ItemIndex := 0;
  cbLine2.ItemIndex := 0;
  cbLine6.ItemIndex := 0;
  cbLineMult.ItemIndex := 0;
  cbLineAcc.ItemIndex := 0;
  rgStripCount.ItemIndex := 0;
  rgStripCountClick(nil);
end;

class function TfmColorDecoder.CalcFriendlyName: string;
begin
  Result := 'Resistor color band decoder';
end;

class procedure TfmColorDecoder.InvokeCalc;
begin
  Application.CreateForm(TfmColorDecoder, fmColorDecoder);
  inherited;
end;

class function TfmColorDecoder.RunKey: string;
begin
  Result := 'color';
end;

procedure TfmColorDecoder.rgStripCountClick(Sender: TObject);
begin
  inherited;
  cbLine2.Visible := rgStripCount.ItemIndex in [1,2];
  cbLine6.Visible := rgStripCount.ItemIndex = 2;
  case rgStripCount.ItemIndex of
    0: begin
         LoadPictureFromRes(imgLine6.Picture, 'CN');
         LoadPictureFromRes(imgLine0.Picture, 'CN');
       end;
    1: begin
         LoadPictureFromRes(imgLine6.Picture, 'CN');
       end;
  end;
  DoLineChange(nil);
end;

procedure TfmColorDecoder.DoLineChange(Sender: TObject);
var tmp: single;
begin
  inherited;
  case rgStripCount.ItemIndex of
    0: // 4-band
       begin
         LoadPictureFromRes(imgLine1.Picture, Format('C%d', [cbLine0.ItemIndex]));
         LoadPictureFromRes(imgLine2.Picture, Format('C%d', [cbLine1.ItemIndex]));
         LoadPictureFromRes(imgLine3.Picture, stResMult[cbLineMult.ItemIndex]);
         LoadPictureFromRes(imgLineAcc.Picture, stResAcc[cbLineAcc.ItemIndex]);
         tmp := cbLine1.ItemIndex+cbLine0.ItemIndex*10;
       end;
    1,2: // 5,6-band
       begin
         LoadPictureFromRes(imgLine0.Picture, Format('C%d', [cbLine0.ItemIndex]));
         LoadPictureFromRes(imgLine1.Picture, Format('C%d', [cbLine1.ItemIndex]));
         LoadPictureFromRes(imgLine2.Picture, Format('C%d', [cbLine2.ItemIndex]));
         LoadPictureFromRes(imgLine3.Picture, stResMult[cbLineMult.ItemIndex]);
         LoadPictureFromRes(imgLineAcc.Picture, stResAcc[cbLineAcc.ItemIndex]);
         tmp := cbLine2.ItemIndex+cbLine1.ItemIndex*10+cbLine0.ItemIndex*100;
         if (rgStripCount.ItemIndex=2) then
           LoadPictureFromRes(imgLine6.Picture, stResPPM[cbLine6.ItemIndex]);
       end;
    else tmp := 0;
  end;
  tmp := tmp*cfMult[cbLineMult.ItemIndex];

  if (tmp<1000) then
    laValue.Caption := Format('%f '+sfUnits[0], [tmp])
  else
  if (tmp<1e6) then
    laValue.Caption := Format('%f '+sfUnits[1], [tmp/1e3])
  else
    laValue.Caption := Format('%f '+sfUnits[2], [tmp/1e6]);
  laValue.Caption := laValue.Caption+Format(', '#$B1'%.f%%', [sfAcc[cbLineAcc.ItemIndex]]);
  if (rgStripCount.ItemIndex=2) then
    laValue.Caption := laValue.Caption+Format(', %.f ppm/K', [sfPPM[cbLine6.ItemIndex]]);
end;

initialization
  RegisterCalc(TfmColorDecoder);

end.
